<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../stores/userStore'
import { 
  School, 
  Tickets, 
  DataAnalysis, 
  Upload, 
  User, 
  Search,
  Calendar,
  OfficeBuilding,
  Management,
  Avatar
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 侧边栏是否折叠
const isCollapse = ref(false)
// 搜索查询
const searchQuery = ref('')

// 根据用户角色获取学校名称显示
const schoolNameDisplay = computed(() => {
  const user = userStore.loginUser
  if (!user) return '学校名称'
  
  // 根据角色显示不同内容
  if (user.role === 2) {
    return '成都市教育局管理员'
  } else if (user.role === 1) {
    return `${user.schoolName}管理员`
  } else if (user.role === 0) {
    return `${user.schoolName} ${user.grade}年级 ${user.classVal}班 班主任`
  }
  
  return '学校名称'
})

// 根据用户角色获取图标
const schoolIcon = computed(() => {
  const user = userStore.loginUser
  if (!user) return School
  
  // 根据角色显示不同图标
  if (user.role === 2) {
    return OfficeBuilding
  } else if (user.role === 1) {
    return Management
  } else if (user.role === 0) {
    return Avatar
  }
  
  return School
})

// 导航菜单项
const navItems = computed(() => [
  { path: '/dashboard', title: schoolNameDisplay.value, icon: schoolIcon.value },
  { path: '/data-entry', title: '数据录入', icon: Tickets },
  { path: '/data-board', title: '数据看板', icon: DataAnalysis },
  { path: '/data-submit', title: '数据提交', icon: Upload },
  { path: '/user-info', title: '信息维护', icon: User }
])

// 跳转到对应页面
const navigateTo = (path) => {
  router.push(path)
}
</script>

<template>
  <div class="main-layout">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-left">
        <el-button 
          type="text" 
          @click="isCollapse = !isCollapse" 
          class="toggle-btn"
        >
          <el-icon><i class="el-icon-menu"></i></el-icon>
        </el-button>
        <div class="logo">
          <el-icon><Calendar /></el-icon>
          <span v-if="!isCollapse">体测数据上报系统</span>
        </div>
      </div>

      <div class="header-center">
        <div class="search-container">
          <el-input
            placeholder="Search"
            prefix-icon="Search"
            v-model="searchQuery"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </div>
      </div>

      <div class="header-right">
        <div class="user-actions">
          <el-button type="text" class="header-icon-btn">
            <el-icon><Calendar /></el-icon>
          </el-button>
          <el-button type="text" class="header-icon-btn user-avatar">
            <el-icon><User /></el-icon>
          </el-button>
        </div>
      </div>
    </header>

    <!-- 主内容区域 -->
    <div class="main-content">
      <!-- 左侧导航栏 -->
      <aside class="sidebar" :class="{ 'is-collapsed': isCollapse }">
        <el-menu
          :collapse="isCollapse"
          router
          :default-active="route.path"
          class="sidebar-menu"
        >
          <el-menu-item 
            v-for="item in navItems" 
            :key="item.path" 
            :index="item.path"
            @click="navigateTo(item.path)"
          >
            <el-icon><component :is="item.icon" /></el-icon>
            <template #title>{{ item.title }}</template>
          </el-menu-item>
        </el-menu>
      </aside>

      <!-- 页面内容 -->
      <main class="page-content">
        <router-view />
      </main>
    </div>
  </div>
</template>

<style scoped>
.main-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* 顶部导航栏样式 */
.header {
  height: 64px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: 600;
  margin-left: 10px;
}

.logo span {
  margin-left: 10px;
}

.toggle-btn {
  font-size: 20px;
  border: none;
  background: transparent;
  cursor: pointer;
}

.header-center {
  flex: 1;
  display: flex;
  justify-content: center;
  max-width: 600px;
}

.search-container {
  width: 100%;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-actions {
  display: flex;
  align-items: center;
}

.header-icon-btn {
  margin-left: 15px;
  font-size: 20px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: #f2f3f5;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 主内容区域样式 */
.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* 侧边栏样式 */
.sidebar {
  width: 220px;
  background-color: #fff;
  box-shadow: 2px 0 4px rgba(0, 0, 0, 0.05);
  transition: width 0.3s;
  z-index: 5;
}

.sidebar.is-collapsed {
  width: 64px;
}

.sidebar-menu {
  height: calc(100vh - 64px);
  border-right: none;
}

.el-menu-item {
  display: flex;
  align-items: center;
}

/* 页面内容样式 */
.page-content {
  flex: 1;
  overflow: auto;
  padding: 20px;
  background-color: #f5f7fa;
}

/* 修复Element Plus菜单样式覆盖 */
:deep(.el-menu-item), :deep(.el-submenu__title) {
  height: 56px;
  line-height: 56px;
}
</style> 